package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.HotelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.HotelVO;
import cn.wolfcode.luowowo.cache.service.IHotelStatisVOService;
import cn.wolfcode.luowowo.comment.domain.HotelComment;
import cn.wolfcode.luowowo.comment.query.HotelCommentQuery;
import cn.wolfcode.luowowo.comment.service.IHotelCommentService;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("hotel")
public class HotelController {

    @Reference
    private IHotelService hotelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IHotelThemeService hotelThemeService;
    @Reference
    private IHotelDestContentService hotelDestContentService;
    @Reference
    private IHotelDetailService hotelDetailService;
    @Reference
    private IHotelStatisVOService hotelStatisVOService;
    @Reference
    private IHotelCommentService hotelCommentService;
    @Reference
    private IHotelOrderService hotelOrderService;
    @RequestMapping("")
    private String hotel(Model model){

        //hotelTags
        List<HotelTheme> hotelTags = hotelThemeService.list();
        if(hotelTags.size()>5){
            hotelTags = hotelTags.subList(0,6);
        }
        model.addAttribute("hotelTags",hotelTags);

        //hotelCity
        model.addAttribute("hotelCity",getHotCitys());
        //dest 国内
        model.addAttribute("dest",getHotCitys());
        return "hotel/hotel";
    }


    @RequestMapping("theme")
    private String theme(Long id,Model model){
        List<Hotel> hotels = hotelService.getHotelByThemeId(id);
        List<HotelVO> list = new ArrayList<>();
        for (Hotel hotel : hotels) {
            //创建vo对象
            HotelVO vo = new HotelVO();
            //获取省份
            Destination province = destinationService.getProvince(hotel.getDest().getId());
            vo.setImg(hotel.getImg());
            vo.setThemeUrl(vo.getThemeUrl(id));
            vo.setShortName(hotel.getDest().getName());
            vo.setName(province.getName());
            list.add(vo);
        }
        model.addAttribute("list",list);
        return "hotel/hotelTpl";
    }


    @RequestMapping("theme1")
    private String theme1(String name,Model model){
        List<Hotel> hotels = hotelService.getHotelByThemeByDestName(name);
        List<HotelVO> list = getVOs(hotels);
        model.addAttribute("hotelScore",list);
        return "hotel/hotelTpl1";
    }


    @RequestMapping("checkeddest")
    @ResponseBody
    private Object checkeddest(String destName,Model model){
        List<Hotel> hotels = hotelService.getHotelByThemeByDestName(destName);
        if (hotels != null && hotels.size()>0){
            return new AjaxResult();
        }
        return new AjaxResult(false);
    }

    @RequestMapping("themes")
    private String themes(@ModelAttribute("qo") HotelQuery qo, Model model){
        //dest
        List<Hotel> hotels = hotelService.getHotelByThemeId(qo.getThemeId());
        List<Destination> citys = new ArrayList<>();
        //获取该主题的区域id
        Long destId = null;
        for (Hotel hotel : hotels) {
            Destination province = destinationService.getProvince(hotel.getDest().getId());
            List<Destination> cityss = destinationService.getCityssByParentId(province.getId());
            citys.addAll(cityss);
            destId = hotel.getDest().getId();
        }
        model.addAttribute("dest",citys);

        //hotcitys
        model.addAttribute("hotcitys",getHotCitys());

        //hotel
        List<HotelVO> hotel = null;
        Destination city = new Destination();
        if(!"中国".equals(qo.getName())){
            List<Hotel> cityHotel = hotelService.getHotelByDestName(qo.getName());
            hotel = getVOs(cityHotel);

            //根据进来的城市初始
            //dh
            city= destinationService.getCityByName(qo.getName());

            //初始区域简介
            model.addAttribute("hs",getHs(city.getId()));

        }else {
            hotel = getVOs(hotels);
            model.addAttribute("hs",getHs(1L));
            city= destinationService.getCityByName(qo.getName());
        }
        model.addAttribute("dh",city);
        model.addAttribute("hotel",hotel);



        //hoteltheme
        List<HotelTheme> themes = hotelThemeService.list();
        model.addAttribute("hoteltheme",themes);
        return "hotel/dingjiudian";
    }

    @RequestMapping("citystrategy")
    private String citystrategy( Long destId, Model model){
        model.addAttribute("hs",getHs(destId));
        return "hotel/citystragetyTpl";
    }

    //获取区域简介
    private HotelVO getHs(Long destId){
        HotelDestContent hotelDestContent = hotelDestContentService.get(destId);
        //创建vo对象
        HotelVO hs = new HotelVO();
        int count = hotelDestContentService.getHotelCountByDestId(hotelDestContent.getDestId());
        hs.setDestId(hotelDestContent.getDestId());
        hs.setChoose(hotelDestContent.getChoose());
        hs.setContent(hotelDestContent.getContent());
        hs.setCount(count);
        return hs;
    }

    //预订酒店列表的异步页面
    @RequestMapping("themelist")
    private String themelist(HotelQuery qo,Model model){

        List<Hotel> hotels = hotelService.getHotelByThemeIdAndDestId(qo);
        //hotel
        List<HotelVO> hotel = getVOs(hotels);
        model.addAttribute("hotel",hotel);
        return "hotel/hotelyudinglistTpl";
    }



    //按主题来获取酒店列表
    private List<HotelVO> getVOs(List<Hotel> hotels){
        List<HotelVO> list = new ArrayList<>();
        for (Hotel hotel : hotels) {
            //创建vo对象
            HotelVO vo = new HotelVO();
            vo.setImg(hotel.getImg());
            vo.setScore(hotel.getScore());
            //进入酒店页面url
            vo.setHotelUrl(vo.getHotelUrl(hotel.getId()));
            vo.setName(hotel.getName());
            vo.setShortName(hotel.getEnglishname());
            vo.setEnglishName(hotel.getEnglishname());
            vo.setAddress(hotel.getAddress());
            vo.setPrice(hotel.getPrice());
            vo.setId(hotel.getId());
            vo.setSummary(hotel.getSummary());
            list.add(vo);
        }
        return list;
    }

    private List<Destination> getHotCitys(){
        List<Destination> dest = destinationService.getCityssByParentId(1L);
        List<Destination> hotelCity = new ArrayList<>();
        for (Destination destination : dest) {
            switch (destination.getName()){
                case HotelVO.CITY_NAME_NANCHANG: hotelCity.add(destination) ; break;
                case HotelVO.CITY_NAME_NANJING : hotelCity.add(destination) ; break;
                case HotelVO.CITY_NAME_GUANGZHOU : hotelCity.add(destination) ; break;
                case HotelVO.CITY_NAME_HANGZHOU : hotelCity.add(destination) ; break;
                case HotelVO.CITY_NAME_SHENZHEN : hotelCity.add(destination) ; break;
                case HotelVO.CITY_NAME_SHIYAN: hotelCity.add(destination) ; break;
            }
        }
        return hotelCity;
    }

    @RequestMapping("detail")
    private String themelist(Long HotelId,Model model){
        //hotel
        Hotel hotel = hotelService.get(HotelId);
        model.addAttribute("hotel",hotel);

        //rooms
        List<HotelDetail> hotelDetails = hotelDetailService.getByHotelId(HotelId);
        model.addAttribute("rooms",hotelDetails);

        //hd
        HotelDetail detail = hotelDetailService.get(HotelId);
        model.addAttribute("hd",detail);

        //zbhotel
        List<Hotel> zbhotels = hotelService.getHotelByThemeByDestName(hotel.getDest().getName());
        List<Hotel> hotelList = new ArrayList<>();
        for (Hotel zbhotel : zbhotels) {
            if(zbhotel.getId()!=HotelId){
                hotelList.add(zbhotel);
            }
        }
        model.addAttribute("zbhotel",hotelList);

        //回显评论数据
        HotelCommentQuery qo = new HotelCommentQuery();
        qo.setHotelId(HotelId);
        qo.setPageSize(Integer.MAX_VALUE);
        Page query = hotelCommentService.query(qo);
        model.addAttribute("list",query.getContent());

        return "hotel/hoteldetail";
    }

    @RequestMapping("room")
    private String room(Long roomId,Model model){
        HotelDetail detail = hotelDetailService.get(roomId);
        model.addAttribute("hd",detail);
        return "hotel/hotelroomdetailTpl";
    }

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long hid,@GetUserInfoAnnotation UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "您还没登录");
            result.setCode(102);
            return result;
        }
        boolean b = hotelStatisVOService.addFavor(hid,userInfo.getId());

        return new AjaxResult(b,null).addData(hotelStatisVOService.getStatisVONums(hid));
    }

    @RequestMapping("commentAdd")
    public String commentAdd(HotelComment comment, Model model, @GetUserInfoAnnotation UserInfo userInfo, int floor){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());


        HotelComment c = hotelCommentService.commentAdd(comment);
        model.addAttribute("c",c);
        Long count = hotelCommentService.count(comment.getHotelId());
        model.addAttribute("floor",count);
        return "hotel/commentTpl";
    }

    @RequestMapping("pay")
    public String pay(Long hid,Long rid, Model model){
        HotelDetail detail = hotelDetailService.get(rid);
        Hotel hotel = hotelService.get(hid);
        HotelVO vo = new HotelVO();
        vo.setName(hotel.getName());
        vo.setRoomType(detail.getName());
        vo.setRoomMoney(detail.getMoney());
        vo.setId(hotel.getId());
        model.addAttribute("hotelPayVo",vo);
        return "hotel/hotelpay";
    }
    @RequestMapping("payok")
    @ResponseBody
    public Object payok(HotelOrder vo,@GetUserInfoAnnotation UserInfo userInfo){
        AjaxResult json = new AjaxResult();
        try{
            hotelOrderService.save(vo);
        }catch(Exception e){
            e.printStackTrace();
        }
            return json;
    }

    @RequestMapping("map")
    public String map(Long hid, Model model){
        Hotel hotel = hotelService.get(hid);
        model.addAttribute("hotel",hotel);
        return "hotel/map";
    }
}
